package com.lgmshare.component.utils;

import android.Manifest;
import android.app.ActivityManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.pm.Signature;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.DisplayMetrics;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.UUID;

/**
 * 运行时上下文环境工具类
 *
 * @author lim
 * @description: TODO
 * @email lgmshare@gmail.com
 * @datetime 2016/7/7 10:02
 */
public class ContextUtils {

    private ContextUtils() {
    }

    /**
     * 获取APP运行环境，APP调试使用
     *
     * @param context
     * @return
     */
    public static String getPhoneInfo(Context context) {
        DisplayMetrics metrics = context.getResources().getDisplayMetrics();
        StringBuilder sb = new StringBuilder();
        sb.append("主板 Build.BOARD :" + Build.BOARD);
        sb.append("\n系统启动程序版本号：Build.BOOTLOADER: " + Build.BOOTLOADER);
        sb.append("\n系统定制商： Build.BRAND：" + Build.BRAND);
        sb.append("\ncpu指令集：Build.CPU_ABI：" + Build.CPU_ABI);
        sb.append("\ncpu指令集2：Build.CPU_ABI2: " + Build.CPU_ABI2);
        sb.append("\n设置参数： Build.DEVICE:" + Build.DEVICE);
        sb.append("\n显示屏参数：Build.DISPLAY:" + Build.DISPLAY);
        sb.append("\n无线电固件版本：Build.getRadioVersion():" + Build.getRadioVersion());
        sb.append("\n硬件识别码： Build.FINGERPRINT:" + Build.FINGERPRINT);
        sb.append("\n硬件名称： Build.HARDWARE:" + Build.HARDWARE);
        sb.append("\nHOST: Build.HOST:" + Build.HOST);
        sb.append("\nID: Build.ID:" + Build.ID);
        sb.append("\n硬件制造商： Build.MANUFACTURER:" + Build.MANUFACTURER);
        sb.append("\n版本： Build.MODEL:" + Build.MODEL);
        sb.append("\n硬件序列号： Build.SERIAL:" + Build.SERIAL);
        sb.append("\n手机制造商：Build.PRODUCT: " + Build.PRODUCT);
        sb.append("\n描述Build的标签： Build.TAGS:" + Build.TAGS);
        sb.append("\nTIME: Build.TIME :" + Build.TIME);
        sb.append("\nBuilder类型 Build.TYPE:" + Build.TYPE);
        sb.append("\nUSER: Build.USER:" + Build.USER);
        sb.append("\nDeviceID: IMEM:" + ContextUtils.getDeviceID(context));
        sb.append("\n分辨率：WIDTH*HEIGHT：").append(metrics.widthPixels + "*" + metrics.heightPixels);
        sb.append("\n屏幕密度：Density：").append(metrics.density);
        sb.append("\nAPP PackageName：").append(ContextUtils.getPackageName(context));
        sb.append("\nAPP VersionName：").append(ContextUtils.getVersionName(context));
        sb.append("\nAPP VersionCode：").append(ContextUtils.getVersionCode(context));
        return sb.toString();
    }

    /**
     * 获取App包名
     *
     * @param context
     * @return app PackageName
     */
    public static String getPackageName(Context context) {
        return context.getPackageName();
    }

    /**
     * 获取进程名
     *
     * @param context
     * @param processId
     * @return
     */
    public static String getProcessName(Context context, int processId) {
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> runningApps = am.getRunningAppProcesses();
        if (runningApps != null) {
            for (ActivityManager.RunningAppProcessInfo info : runningApps) {
                if (info.pid == processId) {
                    return info.processName;
                }
            }
        }
        return null;
    }

    /**
     * 获取App安装包信息
     *
     * @return PackageInfo
     */
    public static PackageInfo getPackageInfo(Context context) {
        return getPackageInfo(context, context.getPackageName());
    }

    /**
     * 获取App信息
     * <p>AppInfo（名称，图标，包名，版本号，版本Code，是否系统应用）</p>
     *
     * @param context     上下文
     * @param packageName 包名
     * @return PackageInfo
     */
    public static PackageInfo getPackageInfo(Context context, String packageName) {
        PackageInfo info = null;
        try {
            info = context.getPackageManager().getPackageInfo(packageName, 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        if (info == null) {
            info = new PackageInfo();
        }
        return info;
    }

    /**
     * 获取App版本号
     *
     * @return versionName
     */
    public static String getVersionName(Context context) {
        PackageInfo pkg = getPackageInfo(context);
        return pkg.versionName;
    }

    /**
     * 获取App版本号
     *
     * @param context     上下文
     * @param packageName 包名
     * @return App版本号
     */
    public static String getVersionName(Context context, String packageName) {
        PackageInfo pkg = getPackageInfo(context, packageName);
        return pkg.versionName;
    }

    /**
     * 获取版本APP VersionCode
     *
     * @param context 上下文
     * @return versionCode
     */
    public static int getVersionCode(Context context) {
        PackageInfo pkg = getPackageInfo(context);
        return pkg.versionCode;
    }

    /**
     * 获取App唯一标识(设备ID，获取不到，则随机生成一个标识)
     * 需要申请权限{@link Manifest.permission#READ_PHONE_STATE}
     *
     * @return DeviceId
     */
    public static String getDeviceID(Context context) {
        String uniqueID = null;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            uniqueID = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            if (PermissionCheckUtils.checkPermission(context, Manifest.permission.READ_PHONE_STATE)) {
                TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
                uniqueID = tm.getMeid();
                if (TextUtils.isEmpty(uniqueID)) {
                    uniqueID = tm.getImei();
                }
                if (TextUtils.isEmpty(uniqueID)) {
                    uniqueID = tm.getMeid(1);
                }
                if (TextUtils.isEmpty(uniqueID)) {
                    uniqueID = tm.getImei(1);
                }
            }
        } else {
            if (PermissionCheckUtils.checkPermission(context, Manifest.permission.READ_PHONE_STATE)) {
                TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
                uniqueID = tm.getDeviceId();
            }
        }
        // 获取不到，则随机生成一个标识
        if ("00000000000000".equals(uniqueID) || "9774d56d682e549c".equals((uniqueID))) {
            uniqueID = null;
        }
        //格式化为形如：e5a3d00a-b4dd-3252-98fc-9633a76dd16d
        if (!TextUtils.isEmpty(uniqueID)) {
            UUID uuid = UUID.nameUUIDFromBytes(uniqueID.getBytes(StandardCharsets.UTF_8));
            uniqueID = uuid.toString();
        }
        if (TextUtils.isEmpty(uniqueID)) {
            uniqueID = getUUID(context);
        }
        if (TextUtils.isEmpty(uniqueID)) {
            uniqueID = UUID.randomUUID().toString();
        }
        return uniqueID;
    }

    private static String getUUID(Context context) {
        String serial = null;
        String m_szDevIDShort = "99" +
                Build.BOARD.length() % 10 + Build.BRAND.length() % 10 +
                Build.CPU_ABI.length() % 10 + Build.DEVICE.length() % 10 +
                Build.DISPLAY.length() % 10 + Build.HOST.length() % 10 +
                Build.ID.length() % 10 + Build.MANUFACTURER.length() % 10 +
                Build.MODEL.length() % 10 + Build.PRODUCT.length() % 10 +
                Build.TAGS.length() % 10 + Build.TYPE.length() % 10 +
                Build.USER.length() % 10; //13 位
        try {
            if (PermissionCheckUtils.checkPermission(context, Manifest.permission.READ_PHONE_STATE)) {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    serial = android.os.Build.getSerial();
                } else {
                    serial = Build.SERIAL;
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        if (TextUtils.isEmpty(serial)) {
            serial = "serial"; // 随便一个初始化
        }
        //使用硬件信息拼凑出来的15位号码
        return new UUID(m_szDevIDShort.hashCode(), serial.hashCode()).toString();
    }

    /**
     * 获取设备名称
     *
     * @return Build.MANUFACTURER + Build.MODEL
     */
    public static String getDeviceName() {
        String manufacturer = Build.MANUFACTURER;
        String model = Build.MODEL;
        if (model.startsWith(manufacturer)) {
            return model;
        } else {
            return manufacturer + " " + model;
        }
    }

    /**
     * 获取应用MD5签名
     *
     * @param context
     * @return
     */
    public static String getSignature(Context context) {
        PackageInfo packageInfo = getPackageInfo(context);
        Signature[] signatures = packageInfo.signatures;
        Signature signature = signatures[0];
        return EncryptUtils.encryptMD5ToString(signature.toByteArray());
    }

    /**
     * 获取manifest文件中的meta标签值
     *
     * @param context
     * @param name
     * @return meta-value
     */
    public static String getManifestMetaDataValue(Context context, String name) {
        ApplicationInfo appInfo = null;
        try {
            appInfo = context.getPackageManager().getApplicationInfo(context.getPackageName(), PackageManager.GET_META_DATA);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        if (appInfo == null) {
            return "";
        }
        return appInfo.metaData.getString(name);
    }

    /**
     * 是否是主进程
     *
     * @param context
     * @return
     */
    public static boolean isMainProcess(Context context) {
        String processName = getProcessName(context, android.os.Process.myPid());
        String packageName = getPackageName(context);
        //是否是主进程
        return packageName.equals(processName);
    }

    /**
     * 判断服务是否运行.
     *
     * @param context
     * @param className 判断的服务名字
     * @return true 在运行 false 未运行
     */
    private boolean isServiceRunning(final Context context, String className) {
        boolean result = false;
        if (context == null || TextUtils.isEmpty(className)) {
            return result;
        }
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        if (am != null) {
            List<ActivityManager.RunningServiceInfo> processInfos = am.getRunningServices(50);
            if (processInfos != null && !processInfos.isEmpty()) {
                for (ActivityManager.RunningServiceInfo processInfo : processInfos) {
                    if (processInfo.service.equals(className)) {
                        result = true;
                    }
                }
            }
        }
        return result;
    }

    /**
     * 判断一个程序是否显示在前端,根据测试此方法执行效率在11毫秒,无需担心此方法的执行效率
     * <p/>
     * 可以根据importance的不同来判断前台或后台
     * RunningAppProcessInfo 里面的常量IMOPORTANCE就是所说的前台后台，其实IMOPORTANCE是表示这个app进程的重要性，
     * 因为系统回收时候，会根据IMOPORTANCE来回收进程的。具体可以去看文档。。
     * public static final int IMPORTANCE_FOREGROUND = 100//在屏幕最前端、可获取到焦点 可理解为Activity生命周期的OnResume();
     * public static final int IMPORTANCE_VISIBLE = 200//在屏幕前端、获取不到焦点可理解为Activity生命周期的OnStart();
     * public static final int IMPORTANCE_SERVICE = 300//在服务中
     * public static final int IMPORTANCE_BACKGROUND = 400//后台
     * public static final int IMPORTANCE_EMPTY = 500//空进程
     * <p/>
     *
     * @param context 上下文环境
     * @return true--->在前端,false--->不在前端
     */
    public static boolean isApplicationShowing(final Context context) {
        boolean result = false;
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        if (am != null) {
            List<ActivityManager.RunningAppProcessInfo> processInfos = am.getRunningAppProcesses();
            if (processInfos != null) {
                for (ActivityManager.RunningAppProcessInfo processInfo : processInfos) {
                    if (processInfo.processName.equals(context.getPackageName())) {
                        int status = processInfo.importance;
                        if (status == ActivityManager.RunningAppProcessInfo.IMPORTANCE_VISIBLE
                                || status == ActivityManager.RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                            result = true;
                        }
                    }
                }
            }
        }
        return result;
    }

    /**
     * 判断应用是否在运行
     *
     * @param context context 上下文环境
     * @return true--->在运行,false--->未运行
     */
    public static boolean isApplicationRunning(final Context context, String packageName) {
        boolean result = false;
        if (context == null || TextUtils.isEmpty(packageName)) {
            return result;
        }
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        if (am != null) {
            List<ActivityManager.RunningAppProcessInfo> processInfos = am.getRunningAppProcesses();
            if (processInfos != null && !processInfos.isEmpty()) {
                for (ActivityManager.RunningAppProcessInfo processInfo : processInfos) {
                    if (processInfo.processName.equals(packageName)) {
                        result = true;
                    }
                }
            }
        }
        return result;
    }

    /**
     * 杀死后台服务进程
     * <p>需添加权限 {@code <uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES"/>}</p>
     *
     * @return 被暂时杀死的服务集合
     */
    public static Set<String> killAllBackgroundProcesses(Context context) {
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> infos = am.getRunningAppProcesses();
        Set<String> set = new HashSet<>();
        for (ActivityManager.RunningAppProcessInfo info : infos) {
            for (String pkg : info.pkgList) {
                am.killBackgroundProcesses(pkg);
                set.add(pkg);
            }
        }
        infos = am.getRunningAppProcesses();
        for (ActivityManager.RunningAppProcessInfo info : infos) {
            for (String pkg : info.pkgList) {
                set.remove(pkg);
            }
        }
        return set;
    }

    /**
     * 杀死后台服务进程
     * <p>需添加权限 {@code <uses-permission android:name="android.permission.KILL_BACKGROUND_PROCESSES"/>}</p>
     *
     * @param packageName 包名
     * @return {@code true}: 杀死成功<br>{@code false}: 杀死失败
     */
    public static boolean killBackgroundProcesses(Context context, String packageName) {
        if (StringUtils.isEmpty(packageName)) return false;
        ActivityManager am = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
        List<ActivityManager.RunningAppProcessInfo> infos = am.getRunningAppProcesses();
        if (infos == null || infos.size() == 0) return true;
        for (ActivityManager.RunningAppProcessInfo info : infos) {
            if (Arrays.asList(info.pkgList).contains(packageName)) {
                am.killBackgroundProcesses(packageName);
            }
        }
        infos = am.getRunningAppProcesses();
        if (infos == null || infos.size() == 0) return true;
        for (ActivityManager.RunningAppProcessInfo info : infos) {
            if (Arrays.asList(info.pkgList).contains(packageName)) {
                return false;
            }
        }
        return true;
    }

    /**
     * 判断应用是否已安装
     *
     * @param context
     * @param packageName
     * @return true已安装  false 未安装
     */
    public static boolean isAppInstalled(Context context, String packageName) {
        final PackageManager packageManager = context.getPackageManager();
        // 获取所有已安装程序的包信息
        List<PackageInfo> packages = packageManager.getInstalledPackages(0);
        for (int i = 0; i < packages.size(); i++) {
            if (packages.get(i).packageName.equalsIgnoreCase(packageName)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 判断应用是否已安装
     *
     * @param context
     * @param intent
     * @return true已安装  false 未安装
     */
    public static boolean isAppInstalled(Context context, Intent intent) {
        final PackageManager packageManager = context.getPackageManager();
        List<ResolveInfo> list = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
        return list.size() > 0;
    }

    /**
     * 安装apk文件
     *
     * @param context
     * @param apkPath
     */
    public static void installApk(Context context, String apkPath) {
        if (AndroidVersionCheckUtils.hasQ()) {
            //适配Android Q,注意apkPath是通过ContentResolver得到的
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setDataAndType(Uri.parse(apkPath), "application/vnd.android.package-archive");
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            context.startActivity(intent);
            return;
        }
        File file = FileUtils.getFile(apkPath);
        if (!FileUtils.isExists(file)) {
            UIUtils.showToast("安装失败，未找不到安装文件");
            return;
        }
        Intent intent = new Intent(Intent.ACTION_VIEW);
        if (AndroidVersionCheckUtils.hasN()) {
            //7.x版本文件访问权限<FileProvider.getUriForFile(context, authority, file)>
            //8.x版本需要用户确认允许安装应用权限<安装应用需要打开未知来源权限，请去设置中开启权限> manifest中配置<REQUEST_INSTALL_PACKAGES>
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);//URI的临时访问权限
            intent.setDataAndType(FileUtils.getUri(context, file), "application/vnd.android.package-archive");
            context.startActivity(intent);
        } else {
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setDataAndType(Uri.fromFile(file), "application/vnd.android.package-archive");
            context.startActivity(intent);
        }
    }

    /**
     * 卸载apk文件
     *
     * @param packageName
     * @param context
     */
    public static void uninstallApk(Context context, String packageName) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        Uri data = Uri.parse("package:" + packageName);
        intent.setData(data);
        context.startActivity(intent);
    }

    /**
     * 清除App所有数据
     *
     * @param context  上下文
     * @param dirPaths 目录路径
     * @return {@code true}: 成功<br>{@code false}: 失败
     */
    public static boolean cleanAppData(Context context, String... dirPaths) {
        File[] dirs = new File[dirPaths.length];
        int i = 0;
        for (String dirPath : dirPaths) {
            dirs[i++] = new File(dirPath);
        }
        return cleanAppData(context, dirs);
    }

    /**
     * 清除App所有数据
     *
     * @param dirs 目录
     * @return {@code true}: 成功<br>{@code false}: 失败
     */
    public static boolean cleanAppData(Context context, File... dirs) {
        boolean isSuccess = FileUtils.cleanInternalCache(context);
        isSuccess &= FileUtils.cleanInternalDbs(context);
        isSuccess &= FileUtils.cleanInternalSP(context);
        isSuccess &= FileUtils.cleanInternalFiles(context);
        isSuccess &= FileUtils.cleanExternalCache(context);
        for (File dir : dirs) {
            isSuccess &= FileUtils.cleanCustomCache(dir);
        }
        return isSuccess;
    }

    /**
     * 调用系统拨号-跳到拨号盘-拨打电话
     *
     * @param context
     * @param phone
     */
    public static void callSystemActionDial(Context context, String phone) {
        Uri uri = Uri.parse("tel:" + phone);
        Intent intent = new Intent(Intent.ACTION_DIAL, uri);
        context.startActivity(intent);
    }

    /**
     * 调用系统拨号-直接拨号
     * TODO 需要增加CALL_PHONE权限
     *
     * @param context
     * @param phone
     */
    public static void callSystemActionCall(Context context, String phone) {
        if (PermissionCheckUtils.checkPermission(context, Manifest.permission.CALL_PHONE)) {
            Uri uri = Uri.parse("tel:" + phone);
            Intent intent = new Intent(Intent.ACTION_CALL, uri);
            context.startActivity(intent);
        } else {
            //没有ACTION_CALL权限,跳到拨号盘
            callSystemActionDial(context, phone);
        }
    }

    /**
     * 调用系统发送短信(发送到指定号码)
     *
     * @param context
     * @param phone   手机号码
     */
    public static void callSystemSmsPhone(Context context, String phone) {
        callSystemSmsAction(context, phone, null);
    }

    /**
     * 调用系统发送短信（发送指定信息）
     *
     * @param context
     * @param content 短信内容
     */
    public static void callSystemSmsMsg(Context context, String content) {
        callSystemSmsAction(context, null, content);
    }

    /**
     * 调用系统发送短信
     *
     * @param context
     * @param phone   手机号码
     * @param content 短信内容
     */
    public static void callSystemSmsAction(Context context, String phone, String content) {
        Uri uri = Uri.parse("smsto:" + (TextUtils.isEmpty(phone) ? "" : phone));
        Intent intent = new Intent(Intent.ACTION_SENDTO, uri);
        intent.putExtra("sms_body", TextUtils.isEmpty(content) ? "" : content);
        context.startActivity(intent);
    }

    /**
     * 发送邮件
     *
     * @param context
     * @param emailReceiver 收件人地址
     * @param subject       邮件默认标题
     * @param content       邮件发送的内容
     */
    public static void sendMail(Context context, String[] emailReceiver, String subject, String content) {
        Intent intent = new Intent(Intent.ACTION_SEND);
        //intent.setType("text/plain"); //模拟器
        intent.setType("message/rfc822"); //真机
        intent.putExtra(Intent.EXTRA_EMAIL, emailReceiver);
        intent.putExtra(Intent.EXTRA_SUBJECT, subject);
        intent.putExtra(Intent.EXTRA_TEXT, content);
        context.startActivity(Intent.createChooser(intent, "Sending mail..."));
    }

    /**
     * 打开一个应用
     *
     * @param context
     * @param packageName
     */
    public static void openApp(final Context context, String packageName) {
        PackageManager packageManager = context.getPackageManager();
        Intent intent = packageManager.getLaunchIntentForPackage(packageName);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
                | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        context.startActivity(intent);
    }

    /**
     * 打开设置首页
     */
    public static void openSetting(Context context) {
        Intent intent = new Intent();
        intent.setAction(Settings.ACTION_SETTINGS);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent);
    }

    /**
     * 打开APP设置详情页面
     */
    public static void openAppSetting(Context context) {
        Intent intent = new Intent();
        intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setData(Uri.fromParts("package", getPackageName(context), null));
        context.startActivity(intent);
    }

    /**
     * 打开无线设置面板
     */
    public static void openWifiSetting(Context context) {
        Intent intent = new Intent();
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setAction(Settings.ACTION_WIFI_SETTINGS);
        context.startActivity(intent);
    }

    /**
     * 打开无线和网络设置面板
     */
    public static void openNetworkSetting(Context context) {
        Intent intent;
        if (android.os.Build.VERSION.SDK_INT > 10) {
            intent = new Intent(Settings.ACTION_WIRELESS_SETTINGS).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        } else {
            intent = new Intent(android.provider.Settings.ACTION_SETTINGS).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }
        context.startActivity(intent);
    }

    /**
     * 打开语言设置面板
     */
    public static void openLanguageSetting(Context context) {
        Intent intent = new Intent();
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setAction(Settings.ACTION_LOCALE_SETTINGS);
        context.startActivity(intent);
    }

    /**
     * 打开位置设置面板
     */
    public static void openLocationSetting(Context context) {
        Intent intent = new Intent();
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        intent.setAction(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
        context.startActivity(intent);
    }

}
